package com.threeonemeeting.adapter;

import java.net.URLEncoder;
import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.threeonemeeting.Const;
import com.threeonemeeting.R;
import com.threeonemeeting.listcache.MyMeetingInfoCache;
import com.threeonemeeting.model.MeetingInfo;
import com.threeonemeeting.tab.BaseTabActivity;
import com.threeonemeeting.util.MiscUtil;

public class MyMeetingInfoListAdapter extends ArrayAdapter<MeetingInfo> {

	protected ArrayList<MeetingInfo> mItemsList;

	public MyMeetingInfoListAdapter(Activity activity) {
		super(activity, 0);
	}

	public void setIconList(ArrayList<MeetingInfo> meetingList) {
		mItemsList = meetingList;
		notifyDataSetChanged();
	}

	public ArrayList<MeetingInfo> getMeetingList() {
		return mItemsList;
	}

	@Override
	public void add(MeetingInfo meetingInfo) {
		if (mItemsList == null)
			mItemsList = new ArrayList<MeetingInfo>();

		mItemsList.add(meetingInfo);
	}

	public void clear() {
		if (mItemsList != null) {
			mItemsList.clear();
		}
	}

	@Override
	public int getCount() {
		if (mItemsList == null)
			return 0;

		return mItemsList.size();
	}

	@Override
	public MeetingInfo getItem(int position) {
		return mItemsList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
	
	public View getView(int position, View convertView, ViewGroup parent) {

		final BaseTabActivity activity = (BaseTabActivity) getContext();
		final MeetingInfo meetingInfoItemData = getItem(position);

		View rowView = convertView;
		MyMeetingInfoCache viewCache;
		if (rowView == null) {
			LayoutInflater inflater = activity.getLayoutInflater();
			rowView = inflater.inflate(R.layout.lvi_my_metting_item, null);
			viewCache = new MyMeetingInfoCache(rowView, meetingInfoItemData.nId);
			rowView.setTag(viewCache);
		} else {
			viewCache = (MyMeetingInfoCache) rowView.getTag();
		}

		// 会议标题
		TextView meetingTitleView = viewCache.getMeetingTitleTextView();
		String strMeetingTitle = meetingInfoItemData.strTitle;
		if (strMeetingTitle != null && strMeetingTitle.length() > 16) {
			strMeetingTitle = strMeetingTitle.substring(0, 16);
			strMeetingTitle += "...";
		}
		meetingTitleView.setText(strMeetingTitle);

		// 会议图片
		ImageView meetingImageView = viewCache.getMeetingImageView();
		String strThumbUrl = Const.URL.BASE_SERVER_URL;
		if (meetingInfoItemData.strOrginalImageUrl != null
				&& meetingInfoItemData.strOrginalImageUrl.equalsIgnoreCase("") == false) {
			strThumbUrl += URLEncoder
					.encode(meetingInfoItemData.strOrginalImageUrl);
			MiscUtil.setImageView(meetingImageView, strThumbUrl);
		} else {
			meetingImageView
					.setBackgroundResource(R.drawable.default_meeting_image);
		}

		// 会议的时间
		TextView meetingTimeView = viewCache.getMeetingTimeTextView();
		meetingTimeView.setText(meetingInfoItemData.strTime);

		// 会议是否免费
		TextView meetingTypeView = viewCache.getMeetingTypeTextView();
		if (meetingInfoItemData.bFree == true) {
			meetingTypeView.setText("免费会议");

		} else {
			meetingTypeView.setText("收费会议");
		}

		// 会议是研讨会还是其余
		TextView meetingCategoryView = viewCache.getMeetingCategoryTextView();
		meetingCategoryView.setText(meetingInfoItemData.strType);

		// 按钮区域

		// 资料按钮
		Button btnMeetingDocs = viewCache.getMeetingDocsBtn();
		if (btnMeetingDocs != null) {
			btnMeetingDocs.setOnClickListener(new OnClickListener() {

				public void onClick(View v) {
					// 查看该会议的资料
					Bundle b = new Bundle();
					b.putInt("id", meetingInfoItemData.nId);
					activity.startActivity("MeetingDataListActivity", b);
				}
			});
		}

		Button btnMeetingComments = viewCache.getMeetingCommentsBtn();
		if (btnMeetingComments != null) {
			btnMeetingComments.setOnClickListener(new OnClickListener() {

				public void onClick(View v) {
					Bundle b = new Bundle();
					b.putInt("id", meetingInfoItemData.nId);
					activity.startActivity("MeetingCommentsListActivity", b);
				}
			});
		}

		Button btnMeetingUsers = viewCache.getMeetingUserBtn();
		if (btnMeetingUsers != null) {
			btnMeetingUsers.setOnClickListener(new OnClickListener() {

				public void onClick(View v) {
					Bundle b = new Bundle();
					b.putInt("id", meetingInfoItemData.nId);
					activity.startActivity("EnrollUserListActivity", b);
				}
			});
		}

		return rowView;
	}

}
